import getXML from '../utils/sameDomain'
import { Router, Route, Switch, Redirect } from 'dva/router';
import { getCookie } from '../utils/auth';
import React, { PureComponent } from 'react';
import { getUserMsg, getUserInfo } from '../services/auth'
import menuTab from '../menuTab'
import ErrorPage from '../routes/404'
import dynamic from "dva/dynamic";
import { connect } from 'dva';
import tabConfig from '../menuTab'
// routerAggregate
class AggreModule extends PureComponent {
    constructor(props, context) {
        super(props)
    }
    render() {
        const { config, location, app } = this.props;
        // test user menu
        /** 
            @ function getCookie auth controller
        */
        const targetRouterConfig = config.find((v) => v.path === location.pathname)
        if (!targetRouterConfig) {
            return (<Redirect to='/404' />)
        } else {
            if (getCookie()) {

                // if success
                if (!localStorage.getItem("info")) {
                    // router AGE api
                    var data = getXML('post', 'http://172.25.253.114:28080/km-gradms-core-server/gradms/base/infoPersonInfo/loadInfoPersonInfoByCurr')
                    if (data.statusCode == 200) {
                        data.data.nationCode = [data.data.nationCode]
                        data.data.peopleCode = [data.data.peopleCode]
                        this.props.userInformation.Info = data.data
                        localStorage.setItem("info", JSON.stringify(data.data))
                    }

                    var msg = getXML('post', 'http://172.25.253.114:28080/km-gradms-core-server/auth/authUser/loadUserInfomation')

                    var menus = getXML('post', 'http://172.25.253.114:28080/km-gradms-core-server/moblile/mobileAuthMenu/loadMenu?appkey=KM_GRADMS_CORE')
                    // 
                    let localMenu = menus.navMenuData.map((s, index) => {
                        return {
                            rel: index,
                            icon: `${s.icon} iconfont`,
                            selectedIcon: `${s.icon} iconfont`,
                            children: s.items == '' ? '' : s.items.map((sChildren, key) => {
                                return {
                                    ...sChildren,
                                    rel: key,
                                    label: sChildren.title,
                                    value: `${index}_${key}`,
                                    children: sChildren.items == '' ? '' : sChildren.items.map((sChildrenIs, keyChildren) => {
                                        return {
                                            ...sChildrenIs,
                                            label: sChildrenIs.title,
                                            value: `${index}_${key}_${keyChildren}`,
                                            rel: keyChildren,
                                        }
                                    }),
                                }
                            }),
                            title: s.title,
                            key: s.mobileUrl.split('/')[1],
                            selected: s.mobileUrl.split('/')[1],
                            onPress: {
                                url: s.mobileUrl,
                                active: s.mobileUrl.split('/')[1]
                            },
                        }
                    })
                    var rows = getXML('post', 'http://172.25.253.114:28080/km-gradms-core-server/gradms/base/baseDatadictionary/loadBaseDatadictionaryListNoPage')
                    var ACE = []
                    var dicObj = {}
                    rows = rows.map(s => {
                        if (s.fatherItemId == 1) {
                            ACE.push(s.id)
                        }
                        return {
                            ...s,
                            path: rows.find(ss => ss.id == s.fatherItemId)
                        }
                    })
                    rows.map(s => {
                        for (let key of ACE) {
                            if (s.fatherItemId == key) {
                                dicObj[s.dataItemNum] = {
                                    id: s.id,
                                    children: {}
                                }
                            }
                        }
                    })
                    rows.map(s => {
                        if (s.id != 1) {
                            if (isNaN(parseInt(s.path.dataItemNum)) && s.path.dataItemNum != 'root' && dicObj[s.path.dataItemNum] != undefined) {
                                // console.log(dicObj[s.path.dataItemNum])
                                dicObj[s.path.dataItemNum].children[s.dataItemNum] = s.dataItemName
                            }
                        }
                    })
                    localStorage.setItem("dic", JSON.stringify(dicObj))
                    this.props.dispatch({ type: "auth/updataWhereInfo", payload: localMenu })
                    this.props.userInformation.Msg = msg
                    localStorage.setItem("msg", JSON.stringify(msg))
                    localStorage.setItem("menu", JSON.stringify(localMenu))
                    this.props.dispatch({
                        type: "auth/updataUserInfo", payload: {
                            Info: data.data,
                            Msg: msg
                        }
                    })
                    // msg.roleName == "学生" ? this.props.dispatch({ type: "auth/updataWhereInfo", payload: tabConfig.student }) : this.props.dispatch({ type: "auth/updataWhereInfo", payload: tabConfig.teacher })
                }
                // set Menu
                // this.props.dispatch({ type: "auth/updataWhereInfo", payload: JSON.parse(localStorage.getItem("menu")) })
                // JSON.parse(localStorage.getItem("msg")).roleName == '学生' ? this.props.dispatch({ type: "auth/updataWhereInfo", payload: tabConfig.student }) : this.props.dispatch({ type: "auth/updataWhereInfo", payload: tabConfig.teacher })
                if (location.pathname == '/login') {
                    return (<Redirect to='/stuIndex' />)
                } else {
                    return (<Route path={location.pathname} component={dynamic({
                        app,
                        models: targetRouterConfig.models, component: targetRouterConfig.component
                    })} />)
                }
            } else {
                const login = config.find((v) => v.path === '/login')
                // logout callBack login
                return (<Route component={dynamic({
                    app,
                    models: login.models, component: login.component
                })} />)
            }

        }


    }
}
function mapStateToProps(state) {
    return {
        userInformation: state.auth.userInformation,
        where: state.auth.where
    }
}
export default connect(mapStateToProps)(AggreModule);
